package org.university.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.university.common.ApiResponse;
import org.university.entity.TeachingClass;
import org.university.exception.ResourceNotFoundException;
import org.university.service.TeachingClassService;

@CrossOrigin
@RestController
@RequestMapping("/teachingClasses")
public class TeachingClassController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TeachingClassController.class);
    private final TeachingClassService teachingClassService;

    public TeachingClassController(TeachingClassService teachingClassService) {
        this.teachingClassService = teachingClassService;
    }

    /**
     * 获取教学班信息分页
     *
     * @param page 页码
     * @param size 页面大小
     * @return 信息分页
     */
    @GetMapping
    public ResponseEntity<ApiResponse<Page<TeachingClass>>> getTeachingClass(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        LOGGER.info("查询教学班：页码={}, 每页大小={}", page, size);
        Page<TeachingClass> teachingClasss = teachingClassService.findAll(PageRequest.of(page, size));
        return ApiResponse.success(teachingClasss);
    }

    /**
     * 根据ID查找教学班
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<TeachingClass>> getTeachingClassById(@PathVariable Integer id) {
        LOGGER.info("查询教学班ID: {}", id);
        return teachingClassService.findById(id)
                .map(ApiResponse::success)
                .orElseThrow(() -> new ResourceNotFoundException("未找到id为：" + id + " 的教学班"));
    }

    /**
     * 创建教学班
     */
    @PostMapping
    public ResponseEntity<ApiResponse<TeachingClass>> createTeachingClass(@RequestBody TeachingClass teachingClass) {
        LOGGER.info("创建教学班ID: {}", teachingClass.getClassId());
        TeachingClass newTeachingClass = teachingClassService.save(teachingClass);
        return ApiResponse.created(newTeachingClass);
    }

    /**
     * 更新教学班信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<TeachingClass>> updateTeachingClass(
            @PathVariable Integer id,
            @RequestBody TeachingClass teachingClass) {

        LOGGER.info("更新教学班ID: {}", id);
        if (teachingClassService.findById(id).isEmpty()) {
            return ApiResponse.error(HttpStatus.NOT_FOUND, new String("未找到id为：" + id + " 的教学班"));
        }
        teachingClass.setClassId(id);
        TeachingClass updatedTeachingClass = teachingClassService.save(teachingClass);
        return ApiResponse.success(updatedTeachingClass);
    }

    /**
     * 删除教学班
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteTeachingClass(@PathVariable Integer id) {
        LOGGER.info("删除教学班ID: {}", id);
        if (teachingClassService.findById(id).isEmpty()) {
            return ApiResponse.error(HttpStatus.NOT_FOUND, new String("未找到id为：" + id + " 的教学班"));
        }
        teachingClassService.deleteById(id);
        return ResponseEntity.ok().build();
    }
}
